<template>
  <div class="page-container">
    <div class="tool-container">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item prop="name">
          <el-input v-model="queryParams.stationName" placeholder="房站名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item prop="status">
          <DictSelect dict="station_leak_status" v-model="queryParams.leakStatus" placeholder="状态" clearable />
        </el-form-item>
        <el-form-item prop="regionCode">
          <RegionSelect v-model="queryParams.regionCode" placeholder="行政区域" clearable />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="times"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD hh:mm:ss"
            value-format="YYYY-MM-DD hh:mm:ss"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['biz:usage:list']">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabel-container">
      <vxe-grid ref="tableRef" v-bind="gridOptions">
        <template #status="{ row }">
          <dict-tag :options="station_usage_status" :value="row.overLoadStatus" />
        </template>
        <template #handle="{ row }">
          <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['biz:usage:query']">详情</el-button>
          <el-button link type="primary" @click="handleSet(row)" v-hasPermi="['biz:usage:edit']">设置</el-button>
        </template>
        <template #bottom>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </template>
      </vxe-grid>
    </div>
    <ExpansionSet ref="refExpansionSet" @success="getList"></ExpansionSet>
  </div>
</template>

<script setup name="StationUsage" lang="ts">
import { listUsageSummary } from '@/api/station/usage/index';
import { BizStationUsageSummaryVo } from '@/api/station/usage/types';
import { getTableConfig } from './columns';
import ExpansionSet from './common/expansion-set/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { station_usage_status } = toRefs<any>(proxy?.useDict('station_usage_status'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);
const refExpansionSet = ref();

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  stationName: '',
  leakStatus: '',
  regionCode: '',
  startFrom: '',
  endTo: ''
});

const times = computed({
  get: () => {
    return [queryParams.value.startFrom, queryParams.value.endTo];
  },
  set: (v) => {
    queryParams.value.startFrom = v[0];
    queryParams.value.endTo = v[0];
  }
});

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listUsageSummary(queryParams.value);
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 修改按钮操作 */
const handleSet = async (row?: BizStationUsageSummaryVo) => {
  refExpansionSet.value?.openSet(row);
};
/** 详情按钮操作 */
const handleDetail = async (row?: BizStationUsageSummaryVo) => {
  proxy?.$tab.openPage('/station/usage/info', '', { id: row?.stationId });
};

onMounted(() => {
  getList();
});
</script>
